package cn.bluemobi.dylan.step.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.step.bean.TiaoShengData;
import cn.bluemobi.dylan.step.step.service.TiaoShengService;
import cn.bluemobi.dylan.step.step.utils.DbUtils;

public class TiaoSheng extends AppCompatActivity {
    public static TextView textView_step;
    public static Button button_start;
    public static Button button_start1;
    public static int step = 0;
    public static boolean processState=false;
    private static int processState1 = 0;
    private static int tt = 0;
    public static String b;
    public static String b1;
    private static String CURRENT_DATE = "";//时间
    private int CURRENT_STEP;//圈数


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tiao_sheng);
        textView_step=(TextView) findViewById(R.id.tv_step);
        button_start=(Button) findViewById(R.id.btn_start);
        button_start1=(Button) findViewById(R.id.btn_start1);
        textView_step.setText(step + "");
        button_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView_step.setText("0");
                if (processState==true || processState1 > 0){
                    button_start.setText("开始");
                    button_start1.setText("暂停");
                    b = "开始";
                    b1 = "暂停";
                    processState1 = 0;
                    tt=0;
                    processState=false;
                    save();
                    Toast.makeText(TiaoSheng.this, "您跳了"+step+"圈！\n记录已保存！", Toast.LENGTH_SHORT).show();
                    stopTSService();
                    startActivity(new Intent(TiaoSheng.this,TiaoShengHistory.class));
                    finish();


                }else{
                    button_start.setText("结束");
                    b = "结束";
                    b1 = "暂停";
                    processState=true;
                    startTSService();
                }
                step=0;
            }
        });
        button_start1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (processState==true){
                    button_start1.setText("继续");
                    b1 = "继续";
                    processState1++;
                    tt++;
                    processState=false;
                }else if(processState==false&&tt>0){
                    button_start1.setText("暂停");
                    b1 = "暂停";
                    tt=0;
                    processState=true;
                }
            }
        });

    }

    private void startTSService(){
        Intent startIntent = new Intent(this, TiaoShengService.class);
        startService(startIntent);
    }

    private void stopTSService(){
        Intent stopIntent = new Intent(this,TiaoShengService.class);
        stopService(stopIntent);
    }

    /**
     * 获取当天日期
     *
     * @return
     */
    private String getTodayDate() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public void save(){
        CURRENT_DATE = getTodayDate();
        TiaoShengData tiaoShengData = new TiaoShengData();
        tiaoShengData.setTime(CURRENT_DATE);
        tiaoShengData.setNumber(step);
        DbUtils.insert(tiaoShengData);
    }

}